package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motion.generator.entity.Feedback;
import com.motion.generator.mapper.FeedbackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    public IPage<Feedback> getFeedbackPage(Long userId, Long page, Long size){
        QueryWrapper<Feedback> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.orderByDesc("createTime");
        return feedbackMapper.selectPage(new Page<>(page, size),queryWrapper);
    }
    public int insertFeedback(Feedback feedback){
        return feedbackMapper.insert(feedback);
    }
    public Feedback getFeedbackById(Long feedbackId){
        return feedbackMapper.selectById(feedbackId);
    }

    public IPage<Feedback> getAllFeedbackPage(Long page, Long size, int type, String input, LocalDateTime starTime,LocalDateTime endTime){

        QueryWrapper<Feedback> queryWrapper=new QueryWrapper<>();
        if (type!=2)
            queryWrapper.eq("ifSolve",type);
        if(starTime!=null)
            queryWrapper.ge("createTime",starTime);
        if (endTime!=null)
            queryWrapper.le("createTime",endTime);
        if (!input.equals(""))
            queryWrapper.like("content",input);
        queryWrapper.orderByDesc("createTime");
        return feedbackMapper.selectPage(new Page<>(page, size),queryWrapper);
    }
    public int updateFeedback(Feedback feedback){
        return feedbackMapper.updateById(feedback);
    }
}
